package com.example.mystoryapp2.ui.user.login

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import com.example.mystoryapp2.R
import com.example.mystoryapp2.contexts.AppContext
import com.example.mystoryapp2.data.network.NetCallBack
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.model.UserModel
import com.example.mystoryapp2.data.network.service.UserInfoService
import com.example.mystoryapp2.databinding.ActivityLoginLayoutBinding
import com.example.mystoryapp2.domain.UserInfo
import com.example.mystoryapp2.ui.base.BaseActivity
import com.example.mystoryapp2.ui.user.regist.RegisterActivity
import com.example.mystoryapp2.ui.utils.DialogUtils
import com.example.mystoryapp2.ui.utils.UserInfoUtils
import retrofit2.Call
import retrofit2.Response

class LoginActivity : BaseActivity<ActivityLoginLayoutBinding, LoginViewModel>() {
    private val TAG = "LoginActivity"
    val userService = ServiceCreator.create<UserInfoService>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun initView() {
        db.btnLogin.setOnClickListener { login() }
        db.btnRegister.setOnClickListener { register() }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_login_layout
    }

    override fun getViewModel(): LoginViewModel {
        return ViewModelProvider(
            this, ViewModelProvider
                .AndroidViewModelFactory(application)
        )[LoginViewModel::class.java]
    }

    override fun setViewModel() {
        db.viewModel = getViewModel()
    }

    private fun login() {
        userService.login(
            UserInfo(
                db.viewModel?.username?.value,
                db.viewModel?.password?.value
            )
        ).enqueue(object : NetCallBack<String>() {
            override fun onResponse(call: Call<String>, response: Response<String>) {
                var body = response.body()
                if (!TextUtils.isEmpty(body)) {
                    Log.d(TAG, "onResponse: " + body)
                    AppContext.saveJwt(body)
                    db.viewModel?.loadUserInfo(object : UserModel.ICallBack {
                        override fun onLoaded() {
                            UserInfoUtils.afterLoginSuccess(body, this@LoginActivity)
                        }

                        override fun onFailed() {
                            DialogUtils.showLongMessage(
                                this@LoginActivity,
                                R.string.message_login_failed
                            )
                        }
                    })
                } else {
                    DialogUtils.showLongMessage(
                        this@LoginActivity,
                        R.string.message_login_failed
                    )
                }
            }
        })
        Log.d(TAG, "login: " + db.viewModel?.username?.value)
        Log.d(TAG, "login: " + db.viewModel?.password?.value)
    }


    private fun register() {
        val intent = Intent(this@LoginActivity, RegisterActivity::class.java)
        startActivity(intent)
    }
}